package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.constant.RedisConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    //将图片名称保存到Redis
    private void savePicNameToRedis(String picName){
        redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_DB_RESOURCES,picName);
    }
    //将图片名称从Redis中删除
    private void deletePicNameFromRedis(String picName){
        redisTemplate.opsForSet().remove(RedisConstant.SETMEAL_PIC_DB_RESOURCES,picName);
    }
    //添加套餐和检查组关联关系
    private void setSetmealAndCheckGroup(Integer setmealId, Integer[] checkgroupIds){
        List<Map<String,Integer>> list= new ArrayList<>();
        if(checkgroupIds != null && checkgroupIds.length > 0){
            for (Integer checkgroupId : checkgroupIds) {
                //封装数据
                Map<String,Integer> map = new HashMap<>();
                map.put("setmealId",setmealId);
                map.put("checkgroupId",checkgroupId);
                list.add(map);
            }
            //调用mapper
            setmealMapper.setSetmealAndCheckGroup(list);
        }
    }
    //删除套餐和检查组关联关系
    private void deleteSetmealAndCheckGroup(Integer id){
        setmealMapper.deleteSetmealAndCheckGroup(id);
    }
    /**
     * 新增套餐
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds){
        log.info("新增套餐关联组数量：{}", checkgroupIds.length);
        //添加套餐基础信息
        setmealMapper.add(setmeal);
        //添加套餐和检查组关联关系
        setSetmealAndCheckGroup(setmeal.getId(),checkgroupIds);
        //从redis中插入图片名称
        if(setmeal.getImg() != null&&setmeal.getImg()!= ""){
            savePicNameToRedis(setmeal.getImg());
        }
    }

    /**
     * 套餐分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean){
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Setmeal> page = setmealMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public Setmeal findById(Integer id){
        return setmealMapper.findById(id);
    }


    /**
     * 编辑套餐
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void edit(Setmeal setmeal,Integer[] checkgroupIds){
        //删除原先套餐和检查组关联关系
        deleteSetmealAndCheckGroup(setmeal.getId());
        //插入新的套餐和检查组关联关系
        setSetmealAndCheckGroup(setmeal.getId(),checkgroupIds);
        ///////////////////////如果更新了图片////////////////////
        if(setmeal.getImg() != null){
            //从redis中删除原先的图片名称
            Setmeal oldSetmeal = setmealMapper.findById(setmeal.getId());
            if(oldSetmeal.getImg() != null&&oldSetmeal.getImg()!= ""){
                deletePicNameFromRedis(oldSetmeal.getImg());
            }
            //从redis中插入新的图片名称
            savePicNameToRedis(setmeal.getImg());
        }
        /////////////////////////////////////////////////////
        setmealMapper.edit(setmeal);

    }

    /**
     * 删除套餐
     * @param id
     */
    @Override
    public void deleteById(Integer id){
        //删除套餐和检查组关联关系
        deleteSetmealAndCheckGroup(id);
        //从redis中删除图片名称
        Setmeal Setmeal = setmealMapper.findById(id);
        if(Setmeal.getImg() != null&&Setmeal.getImg()!= ""){
            deletePicNameFromRedis(Setmeal.getImg());
        }
        //删除套餐
        setmealMapper.deleteById(id);

    }
}
